package dfsketches.visualizer_2d;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;

import javax.swing.JFrame;

import dfsketches.visualizer_2d.WindowCloser;

public class VisualizerWindow extends JFrame {

	private static final long serialVersionUID = -1955162665659594417L;

	public VisualizerWindow(int[][] forDraw) {
		this(forDraw, new ColorSolver() {
			@Override
			public int getColor(int value) {
				return value;
			}
		});
	}
	
	public VisualizerWindow(int[][] forDraw, int posX, int posY) {
		this(forDraw, new ColorSolver() {
			@Override
			public int getColor(int value) {
				return value;
			}
		});
		setLocation(posX, posY);
	}
	
	public VisualizerWindow(int[][] forDraw, ColorSolver solver) {
		super();

		terrain = forDraw;
		h = terrain.length;
		w = terrain[0].length;
		bitmap = new int[w * h];
		
		setName("Plotter");
		setTitle("Terrain Generator");
		setSize(w + 10, h + 40);
		setVisible(true);
		
		image = createImage(new MemoryImageSource(w, h, bitmap, 0, h));
		colorer = solver;
		paint(getGraphics());
		addWindowListener(new WindowCloser());
		
		draw();
	}
	
	public void draw() {
		int t = 0;
		for (int i = 0; i < h; i++) {
			int[] terrainI = terrain[i];
			for (int j = 0; j < w; j++) {
				bitmap[t++] = colorer.getColor(terrainI[j]);
			}
		}
		image = createImage(new MemoryImageSource(w, h, bitmap, 0, w));
		Graphics gr = getGraphics();
		paint(gr);
	}
	
	public void paint(Graphics g) {
		g.drawImage(image, 5, 30, this);
	}
	
	private Image image;
	private int w;
	private int h;
	private int[][] terrain;
	private int[] bitmap;
	private ColorSolver colorer;
}